import { eventBus } from "../../utils/util"

Page({

  data: {
    isLoggedIn: false,
    openid: ""
  },

  onLoad() {
    // '普通会员'
    // '银卡会员'
    // '金卡会员'
    // '钻石会员'
    this.initMemberBenefits('钻石会员')
  },
  login() {
    if (this.data.isLoggedIn) {
      wx.showToast({
        title: '您已登录',
        icon: 'none'
      })
      return
    }
    wx.cloud.callFunction({
      name: 'login_register',
      success: res => {
        console.log(res)
      },
      fail: err => {
        console.error('注册失败', err)
      }
    })

  },
  // 初始化会员权益数据
  initMemberBenefits(levelName: string) {
    // 根据会员等级设置不同的权益
    const benefitsMap: Record<string, Array<{
      id: string,
      name: string,
      icon: string,
      description: string
    }>> = {
      '普通会员': [
        {
          id: 'discount',
          name: '会员折扣',
          icon: 'discount-filled',
          description: '享受全场商品9.5折优惠。'
        },
        {
          id: 'points',
          name: '积分奖励',
          icon: 'star-filled',
          description: '购物即可获得积分，1元=1积分。'
        },
        {
          id: 'birthday',
          name: '生日礼遇',
          icon: 'cake-filled',
          description: '生日当月获赠生日饮品券一张。'
        }
      ],
      '银卡会员': [
        {
          id: 'discount',
          name: '会员折扣',
          icon: 'discount-filled',
          description: '享受全场商品9折优惠。'
        },
        {
          id: 'points',
          name: '积分奖励',
          icon: 'star-filled',
          description: '购物即可获得积分，1元=1.2积分。'
        },
        {
          id: 'birthday',
          name: '生日礼遇',
          icon: 'cake-filled',
          description: '生日当月获赠生日饮品券两张。'
        },
        {
          id: 'free_delivery',
          name: '免费配送',
          icon: 'undertake-delivery-filled',
          description: '单笔订单满50元享受免费配送服务。'
        }
      ],
      '金卡会员': [
        {
          id: 'discount',
          name: '会员折扣',
          icon: 'discount-filled',
          description: '享受全场商品8.5折优惠。'
        },
        {
          id: 'points',
          name: '积分奖励',
          icon: 'star-filled',
          description: '购物即可获得积分，1元=1.5积分。'
        },
        {
          id: 'birthday',
          name: '生日礼遇',
          icon: 'cake-filled',
          description: '生日当月获赠生日饮品券三张。'
        },
        {
          id: 'free_delivery',
          name: '免费配送',
          icon: 'undertake-delivery-filled',
          description: '单笔订单满30元享受免费配送服务。'
        },
        {
          id: 'priority',
          name: '优先制作',
          icon: 'time-filled',
          description: '门店订单优先制作，节省等待时间。'
        }
      ],
      '钻石会员': [
        {
          id: 'discount',
          name: '会员折扣',
          icon: 'discount-filled',
          description: '享受全场商品8折优惠。'
        },
        {
          id: 'points',
          name: '积分奖励',
          icon: 'star-filled',
          description: '购物即可获得积分，1元=2积分。'
        },
        {
          id: 'birthday',
          name: '生日礼遇',
          icon: 'cake-filled',
          description: '生日当月获赠生日饮品券五张。'
        },
        {
          id: 'free_delivery',
          name: '免费配送',
          icon: 'undertake-delivery-filled',
          description: '所有订单享受免费配送服务。'
        },
        {
          id: 'priority',
          name: '优先制作',
          icon: 'time-filled',
          description: '门店订单优先制作，节省等待时间。'
        },
        {
          id: 'exclusive',
          name: '专属礼遇',
          icon: 'gift-filled',
          description: '每月获赠一张钻石会员专属饮品券。'
        }
      ]
    }

    // 设置默认权益
    let benefits = benefitsMap['普通会员'] || []

    // 根据会员等级设置权益
    if (benefitsMap[levelName]) {
      benefits = benefitsMap[levelName]
    }

    this.setData({
      memberBenefits: benefits
    })
  },
})